package restless.core.managers {
	
	import restless.core.gameObjs.RLEUpdateRenderObj;
	//import restless.core.RLEMSTimer;
	import restless.core.RLETickTimer;
	import restless.core.interfaces.ICreateTimers;
	import restless.core.interfaces.ITimer;
	import restless.core.interfaces.IUR;
	
	/**
	 * ...
	 * @author Jordan Laine, Must Not Sleep, LLC
	 */
	
	public class RLETimerManager extends RLEUpdateRenderObj {
		
		private var vid:Vector.<String>;
		private var vTimers:Vector.<ITimer>;
		private var tempTimer:ITimer;
		private var i:int;
		
		public function RLETimerManager() {
			setRE();
			vid = new Vector.<String>;
			vTimers = new Vector.<ITimer>;
			RE.addToUpdate(this);
		}
		
		override public function update():void {
			i = vTimers.length -1;;
			for (i; i > -1; i--) {
				vTimers[i].render();
			}
		}
		
		/*public function addMSTimer(id:String, timeToWait:int, obj:ICreateTimers, loop:Boolean = false, manualDispose:Boolean = false):ITimer {
			var timer:RLEMSTimer = new RLEMSTimer(id, timeToWait, obj, this, loop, manualDispose);
			vTimers.push(timer);
			return timer;
		}*/
		
		public function addTickTimer(id:String, timeToWait:int, obj:ICreateTimers, loop:Boolean=false, manualDispose:Boolean=false):ITimer {			
			var timer:RLETickTimer = new RLETickTimer(id, timeToWait, obj, this, loop, manualDispose); 
			vTimers.push(timer);
			return timer;
		}
		
		public function addRandomTickTimer(id:String, timeToWait:int, loop:Boolean = false, manualDispose:Boolean = false):void {
			
		}
		
		public function findTimerByID(string:String):ITimer {
			i = vTimers.length -1;
			var id:String = string;
			for (i; i > -1; i--) {
				tempTimer = vTimers[i];
				if (tempTimer.sID == id) {
					return tempTimer;
				}
			}
			trace("No Timer Found");
			return null;
		}
		
		public function resetTimer(timer:ITimer):void {
			timer.resetTime();
		}
		
		public function resetTimerByID(id:String):void {
			findTimerByID(id).resetTime();
		}
		
		public function removeTimer(timer:ITimer):void {
			var _id:int = vTimers.indexOf(timer);
			vTimers.splice(_id, 1);
		}
		
		public function removeTimerByID(id:String):void {
			removeTimer( findTimerByID(id) );
		}
		
		public function removeTimersByArray(list:Vector.<ITimer>):void {
			i = list.length;
			if (i > 0) {
				i--;
				var tempV:Vector.<ITimer> = list;
				for (i; i >= 0; i--) {
					tempTimer = tempV[i];
					tempTimer.dispose();
				}
				tempTimer = null;
				tempV.length = 0;
			}
		}
		
		//SLOW
		public function removeTimersAssociatedWithObj(obj:ICreateTimers):void {
			i = vTimers.length -1;
			var tempObj1:ICreateTimers = obj;
			var tempObj2:ICreateTimers;
			for (i; i > -1; i--) {
				tempTimer = vTimers[i];
				tempObj2 = tempTimer.obj;
				if (tempObj1 == tempObj2) {
					tempTimer.dispose();
					tempObj1 = null;
					tempObj2 = null;
					vTimers.slice(i, 1);
					tempTimer = null;
				}
			}
		}
		
		public function removeAllTimers():void {
			i = vTimers.length -1;
			if (i > -1) {
				for (i; i > -1; i--) {
					tempTimer = vTimers[i];
					tempTimer.dispose();
					tempTimer = null;
				}
			}
		}
		
		override public function dispose():void {
			removeAllTimers();
			super.dispose();
		}
	}
}